package org.wyy.vm.dao.impl;

import org.wyy.vm.cache.VMCache;
import org.wyy.vm.dao.IUserDao;
import org.wyy.vm.entity.User;

/**
 * @author w
 *
 */
public class UserDaoImpl implements IUserDao {

	@Override
	public User getById(long id) {
		for (User u : VMCache.USER_LIST) {
			if (u.getId() == id) {
				return u;
			}
		}
		return null;
	}

	@Override
	public void add(User u) {
		VMCache.USER_LIST.add(u);
		VMCache.saveUser();
	}

	@Override
	public void delete(long id) {
		VMCache.USER_LIST.removeIf(v -> v.getId() == id);
		VMCache.saveUser();
	}

	@Override
	public void update(User u) {
		User user = getById(u.getId());
		user.setName(u.getName());
		user.setPassword(u.getPassword());
		VMCache.saveUser();
	}

	@Override
	public User getByName(String name) {
		for (User u : VMCache.USER_LIST) {
			if (u.getName().equals(name)) {
				return u;
			}
		}
		return null;
	}


}
